from PySide6.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton
from PySide6.QtCore import Qt
import os

class TemplateEditDialog(QDialog):
    def __init__(self, template_name='', template_content='', parent=None):
        super().__init__(parent)
        self.template_name = template_name
        self.template_content = template_content
        self.template_dir = os.path.expanduser('~/.a-terminal/templates')
        self.init_ui()
    
    def init_ui(self):
        """初始化UI"""
        self.setWindowTitle('编辑模板')
        self.resize(600, 400)
        
        layout = QVBoxLayout(self)
        layout.setContentsMargins(16, 16, 16, 16)
        layout.setSpacing(16)
        
        # 模板名称输入框
        name_layout = QHBoxLayout()
        name_label = QLabel('模板名称：')
        self.name_input = QLineEdit(self.template_name)
        name_layout.addWidget(name_label)
        name_layout.addWidget(self.name_input)
        layout.addLayout(name_layout)
        
        # 模板内容编辑框
        content_label = QLabel('模板内容：')
        layout.addWidget(content_label)
        
        self.content_edit = QTextEdit()
        self.content_edit.setPlainText(self.template_content)
        layout.addWidget(self.content_edit)
        
        # 按钮区域
        button_layout = QHBoxLayout()
        button_layout.setContentsMargins(0, 16, 0, 0)
        button_layout.setSpacing(8)
        
        self.save_btn = QPushButton('保存')
        self.save_btn.clicked.connect(self.save_template)
        
        self.cancel_btn = QPushButton('取消')
        self.cancel_btn.clicked.connect(self.reject)
        
        button_layout.addStretch()
        button_layout.addWidget(self.cancel_btn)
        button_layout.addWidget(self.save_btn)
        
        layout.addLayout(button_layout)
    
    def save_template(self):
        """保存模板"""
        new_name = self.name_input.text().strip()
        new_content = self.content_edit.toPlainText()
        
        if not new_name:
            return
        
        # 如果名称发生变化，需要删除旧文件并创建新文件
        if new_name != self.template_name and self.template_name:
            old_path = os.path.join(self.template_dir, f'{self.template_name}.txt')
            if os.path.exists(old_path):
                os.remove(old_path)
        
        # 保存新文件
        new_path = os.path.join(self.template_dir, f'{new_name}.txt')
        with open(new_path, 'w', encoding='utf-8') as f:
            f.write(new_content)
        
        self.template_name = new_name
        self.template_content = new_content
        self.accept()
    
    @classmethod
    def edit_template(cls, template_name, parent=None):
        """静态方法，用于编辑模板"""
        template_dir = os.path.expanduser('~/.a-terminal/templates')
        template_path = os.path.join(template_dir, f'{template_name}.txt')
        
        template_content = ''
        if os.path.exists(template_path):
            with open(template_path, 'r', encoding='utf-8') as f:
                template_content = f.read()
        
        dialog = cls(template_name, template_content, parent)
        result = dialog.exec()
        
        if result == QDialog.DialogCode.Accepted:
            return dialog.template_name, dialog.template_content
        return None, None